<script setup lang="ts">
import { getStatisticsInfoApi, getStatisticsTotalApi } from "@/api/statistics";
import dayjs from "dayjs";
import { reactive, ref } from "vue";
import { Upload } from "@element-plus/icons-vue";
import { message } from "@/utils/message";
import { useUserStore } from "@/store/modules/user";

defineOptions({
  name: "Statistics"
});

const formInline = reactive({
  user: "",
  region: "",
  date: ""
});

const summaries = ref([]);
const getSummaries = () => {
  return summaries.value;
};
const userId = useUserStore().userId;
async function getSummariesAsync() {
  const res = await getStatisticsTotalApi({
    ...formData.value,
    userId
  });

  summaries.value[0] = "合计";
  summaries.value[1] = "";
  summaries.value[2] = res.data.frequency.toString();
  summaries.value[3] = res.data.amount.toString();
}

// 表单数据
const formData = ref({
  type: "福彩",
  date: dayjs().format("YYYY-MM-DD")
});

const pageInfo = ref({
  page: 1,
  pageSize: 11,
  total: 0
});
// 表格数据
const tableData = ref();

const locationOptions = ["福彩", "体彩"];

// 查询列表
const queryStatisticsList = async () => {
  const res = await getStatisticsInfoApi({
    ...pageInfo.value,
    ...formData.value,
    userId
  });
  tableData.value = res.data.records;
  pageInfo.value.total = res.data.total;
  getSummariesAsync();
  message("号码统计查询成功", { type: "success" });
};
queryStatisticsList();

// 页面变化函数
const currentChange = pageNum => {
  pageInfo.value.page = pageNum;
  queryStatisticsList();
};

const pervClick = () => {
  pageInfo.value.page = pageInfo.value.page - 1;
  queryStatisticsList();
};
const nextClick = () => {
  pageInfo.value.page = pageInfo.value.page + 1;
  queryStatisticsList();
};
</script>

<template>
  <div class="common-layout">
    <el-container>
      <el-header class="header">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <!-- <el-form-item label="Approved by">
            <el-input
              v-model="formInline.user"
              placeholder="Approved by"
              clearable
            />
          </el-form-item> -->
          <el-form-item label="彩票类型" prop="location">
            <el-segmented v-model="formData.type" :options="locationOptions" />
          </el-form-item>
          <el-form-item label="日期">
            <el-date-picker
              v-model="formData.date"
              type="date"
              placeholder="选择日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="queryStatisticsList"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
        <el-divider style="margin: 0" />
        <el-button type="primary">
          导出<el-icon class="el-icon--right"><Upload /></el-icon>
        </el-button>
      </el-header>
      <el-main class="main">
        <el-table :data="tableData" :summary-method="getSummaries" show-summary>
          <el-table-column prop="date" label="日期" width="180" />
          <el-table-column prop="number" label="号码" />
          <el-table-column prop="frequency" label="注数" />
          <el-table-column prop="amount" label="金额" />
        </el-table>
        <el-pagination
          background
          layout="prev, pager, next"
          :total="pageInfo.total"
          :page-size="pageInfo.pageSize"
          class="mt-4"
          @current-change="currentChange"
          @prev-click="pervClick"
          @next-click="nextClick"
        />
      </el-main>
    </el-container>
  </div>
</template>

<style scoped lang="scss">
.header {
  padding-top: 10px;
  background-color: #fff;
  height: 180px;
  line-height: 80px;
  margin-bottom: 15px;
}
.main {
  padding: 0;
}
.mt-4 {
  padding-right: 100px;
  height: 50px;
  margin-top: 0;
  background: #fff;
}
</style>
